﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Micua.Extension
{
    /// <summary>
    /// HtmlHelper扩展方法类
    /// </summary>
    public static class Html
    {
        /// <summary>
        /// 生成富文本编辑器(UEditor)
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static MvcHtmlString UEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");
            var data=ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var htmlStr = new MvcHtmlString(string.Format(
                "<script type=\"text/javascript\" charset=\"utf-8\" src=\"/Content/UEditor/ueditor.config.js\"></script>\r\n<script type=\"text/javascript\" charset=\"utf-8\" src=\"/Content/UEditor/ueditor.all.min.js\"></script>\r\n{0}\r\n<script type=\"text/javascript\">\r\nvar ue = UE.getEditor('{1}',{{wordCountMsg:'字数：{{#count}}&nbsp;&nbsp;', autoClearinitialContent: false, initialFrameHeight: 300}});\r\n</script>",
                htmlHelper.TextAreaFor(expression), 
                data.PropertyName));
            return htmlStr;
        }
    }
}
